package com.gec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gec.entity.*;
import com.gec.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysServiceImpl implements SysService {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @Override
    public List<SysPermission> findpermissionsByUserId(String username) {
        //获取用户角色
        QueryWrapper<SysUserRole> ew1 = new QueryWrapper<>();
        ew1.eq("sys_user_id", username);
        SysUserRole userRole = sysUserRoleService.getOne(ew1);

        //获取角色对应的权限
        QueryWrapper<SysRolePermission> ew2 = new QueryWrapper<>();
        ew2.eq("sys_role_id", userRole.getSysRoleId());
        List<SysRolePermission> rolePermissions = sysRolePermissionService.list2(ew2);

        List<SysPermission> permissions = new ArrayList<>();

        for (SysRolePermission rolePermission : rolePermissions) {
            QueryWrapper<SysPermission> ew3 = new QueryWrapper<>();
            //通过权限id获取权限
            ew3.eq("id", rolePermission.getSysPermissionId());
            SysPermission permission = sysPermissionService.getOne(ew3);
            //依次放入集合中
            permissions.add(permission);
        }

        return permissions;
    }

    @Override
    public List<MenuTree> getMenuTree() {
        QueryWrapper<SysPermission> ew = new QueryWrapper<>();
        ew.eq("parentid", 1).select("id", "name");
        List<SysPermission> permissions = sysPermissionService.list(ew);
        List<MenuTree> menuTrees = new ArrayList<>();
        for (SysPermission permission : permissions) {
            MenuTree menuTree = new MenuTree();
            menuTree.setId(Integer.parseInt(String.valueOf(permission.getId())));
            menuTree.setName(permission.getName());
            menuTrees.add(menuTree);
        }
        //找出根菜单下的子菜单
        for (MenuTree menuTree : menuTrees) {
            QueryWrapper<SysPermission> ew2 = new QueryWrapper<>();
            ew2.eq("parentid", menuTree.getId());
            List<SysPermission> sysPermissions = sysPermissionService.list(ew2);
            menuTree.setChildren(sysPermissions);
        }

        return menuTrees;
    }

    @Override
    public List<SysRole> findAllRoles() {

        return sysRoleService.list();
    }

    @Override
    public List<SysPermission> findpermissionsByRoleId(String role_id) {

        //获取角色对应的权限
        QueryWrapper<SysRolePermission> ew = new QueryWrapper<>();
        ew.eq("sys_role_id", role_id);

        List<SysRolePermission> rolePermissions = sysRolePermissionService.list2(ew);

        List<SysPermission> permissions = new ArrayList<>();

        for (SysRolePermission rolePermission : rolePermissions) {
            QueryWrapper<SysPermission> ew2 = new QueryWrapper<>();
            //通过权限id获取权限
            ew2.eq("id", rolePermission.getSysPermissionId());
            SysPermission permission = sysPermissionService.getOne(ew2);
            //依次放入集合中
            permissions.add(permission);
        }

        return permissions;
    }


}
